﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

using ECCIT.Extension.Weibo_v2.Model;
using ECCIT.Extension.Weibo_v2.Common;
using ECCIT;
using ECCIT.Web;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Configuration;
using OgilvyOne.UKNow.Mobile.Utilities;

namespace OgilvyOne.UKNow.Mobile.APIs.Comments
{
    public class Submit : IHttpHandler
    {
        private static readonly int S_LANG_ID = 2052;

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                EventsCommentModel comment = ValidateArguments(context);
                if (comment == null) return;

                comment.ImageName = SaveImage(context);

                EventsCommentBLL bll = new EventsCommentBLL(S_LANG_ID);
                ResultInfo result = bll.Save(comment);
                if (result.Result)
                {
                    EventsCommentModel model = (EventsCommentModel)result.ReturnObject;
                    EventComment comm = new EventComment();
                    comm.MEMBER_ID = model.MemberID;
                    comm.COL_COMMENT_IMG_NAME = string.Empty;
                    if (!string.IsNullOrEmpty(model.ImageName))
                        comm.COL_COMMENT_IMG_NAME = ConfigCacheManager.DomainName + ConfigCacheManager.FilePath.UploadImagePath.VirtualPath + "/" + model.ImageName;
                    comm.COL_COMMENT_TEXT = model.Content;
                    comm.COL_COMMENT_TIME = model.Time.ToString("yyyy/MM/dd HH:mm:ss");
                    comm.COL_EVENTS_ID = model.EventsID;
                    comm.COL_EVENTS_TITLE = "";

                    string s = JsonSerializationHelper.ObjectToJson(comm);
                    context.Response.Write(ResultUtility.ReturnResultJson("0", s, "发表评论成功"));
                }
                else
                {
                    context.Response.Write(ResultUtility.ReturnResultJson("-101", "", "发表评论失败. Message:" + result.Message));
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-101", "", "发表评论失败. Message:" + ex.Message));
            }
        }

        private EventsCommentModel ValidateArguments(HttpContext context)
        {
            string sUserID = context.Request.Params["uid"];
            if (string.IsNullOrEmpty(sUserID)) throw new ArgumentNullException("uid");

            string sEventID = context.Request.Params["eventid"];
            if (string.IsNullOrEmpty(sEventID)) throw new ArgumentNullException("eventid");

            string text = context.Request.Params["text"];
            if (string.IsNullOrEmpty(text)) throw new ArgumentNullException("text");

            EventsCommentModel comment = new EventsCommentModel();
            comment.LanguageID = S_LANG_ID;
            comment.ID = null;
            comment.EventsID = int.Parse(sEventID);
            comment.MemberID = int.Parse(sUserID);
            comment.Content = text;
            comment.Time = DateTime.Now;
            return comment;
        }

        private string SaveImage(HttpContext context)
        {
            try
            {
                HttpPostedFile file = context.Request.Files["pic"];
                if (file == null) return string.Empty;

                ResultInfo result = UploadedFile.Save(file, ConfigCacheManager.FilePath.UploadFrontImagePath, UploadedFile.RenameHandlers.RenameToGuid);
                if (result.Result)
                {
                    return ((FileInfo)result.ReturnObject).Name;
                }
                else
                {
                    if (result.Exception != null)
                        throw result.Exception;
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("保存评论图片时发生异常。错误信息：" + ex.Message);
            }
        }
    }
}
